#include "PowerMonitor.h"

//<POWER_([0-9.]+)_\\[A/([0-9]{1,3})/([0-9.]{1,9})]\\[B/([0-9]{1,3})/([0-9.]{1,9})]\\[C/([0-9]{1,3})/([0-9.]{1,9})](|\\[([0-9.]+)]\\[([0-9.]+)]\\[([0-9.]+)])>
const std::regex PowerMonitor::POWER_PATTERN("<POWER_([0-9.]+)_\\[A/([0-9]{1,3})/([0-9.]{1,9})\\]\\[B/([0-9]{1,3})/([0-9.]{1,9})\\]\\[C/([0-9]{1,3})/([0-9.]{1,9})\\]_([0-9.]+)_([0-9.]+)_([0-9.]+)>");



bool PowerMonitor::updateInformation() {
    bool updateResult = false;
    std::string result;

    mchannel.sendStringReturnString(std::string("<POWER>"), result);

    std::smatch match;
    if ( std::regex_match(result, match, POWER_PATTERN)) {
        updateResult = true;

        mkwh = match.str(1);

        maPhase.voltage = match.str(2);
        maPhase.current = match.str(3);

        mbPhase.voltage = match.str(4);
        mbPhase.current = match.str(5);

        mcPhase.voltage = match.str(6);
        mcPhase.current = match.str(7);

        mpower =  match.str(8);

        mgridFrequency = match.str(9);
        mcoefficient = match.str(10);
    }
    return updateResult;
}


